package com.pk.platform.business.data.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.pk.platform.business.core.dao.impl.GenericDaoImpl;
import com.pk.platform.business.data.dao.IBaseDataDao;
import com.pk.platform.business.data.vo.BaseDataVO;
import com.pk.platform.domain.data.BaseData;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class BaseDataDaoImpl extends GenericDaoImpl implements IBaseDataDao {

	public ListPage queryBaseData(BaseDataVO bvo, Pager pager) {
		StringBuffer sql = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		sql.append("select b.*, (select count(id) from p_baseData b2 where b2.parentId = b.id) as childrenCount from p_baseData b where 1 = 1");
		if(bvo.getBd().getParentId() != null && !"".equals(bvo.getBd().getParentId()))
		{
			sql.append(" and b.parentId = ?");
			param.add(bvo.getBd().getParentId());
		}
		if(bvo.getBd().getId() != null && !"".equals(bvo.getBd().getId()))
		{
			sql.append(" and b.id like ?");
			param.add("%" + bvo.getBd().getId() + "%");
		}
		if(bvo.getBd().getDataValue() != null && !"".equals(bvo.getBd().getDataValue()))
		{
			sql.append(" and b.dataValue like ?");
			param.add("%" + bvo.getBd().getDataValue() + "%");
		}
		sql.append(" order by b.sort");
		return this.getListPage(sql.toString(), param.toArray(), pager);
	}

	public List<BaseData> queryBaseDataByParentId(String parentId) {
		String hql = "from BaseData bd where bd.parentId = ? order by bd.sort";
		return this.getHibernateTemplate().find(hql, new Object[]{parentId});
	}

}
